package com.sl.au.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.BussinessLevel;


@Transactional(readOnly = true)
public interface BussinessLevelRepository extends JpaRepository<BussinessLevel, String> {
	
	@Query(" select bl from BussinessLevel bl where bl.levelParentId is not null order by bl.levelOrder")
	List<BussinessLevel> findAll();
	
	/**
	 * 商户区域的快速查询
	 * 
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query(" select bl from BussinessLevel bl where bl.levelParentId is null and bl.levelName like %?1% order by bl.levelOrder")
	Page<BussinessLevel> getBussinessLevelName(String key, Pageable pageable);
	
	/**
	 * 得到商户区域数据条数
	 * @param key
	 * @return
	 */
	@Query(" select count(*) as num from BussinessLevel bl where bl.levelParentId is null and bl.levelName like %?1%")
	Long getBussinessLevelCount(String key);
	
	@Query(" select bl from BussinessLevel bl where bl.levelParentId is null order by bl.levelOrder")
	Page<BussinessLevel> getData(Pageable pageable);
	
	/**
	 * 得到商户区域数据条数
	 * @param key
	 * @return
	 */
	@Query("select count(*) as num from BussinessLevel bl where bl.levelParentId is null")
	Long getCount();
	
	@Modifying
	@Query("update BussinessLevel bl set bl.levelParentId = null where bl.id=?1")
	int setBussinessLevel(String id);

	@Query("select bl from BussinessLevel bl where bl.levelName=?1")
	BussinessLevel findByLevelName(String levelName);
	
}
